<template>
  <div>
    <!-- 线索热度定义弹窗 -->
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="1"
        :formType="type"
        labelWidth="110"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">确 定</AjaxButton>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import {
  getIdParameter,
  addClueParameter,
  editClueParameter,
  queryClueSourceList
} from '@/api/potenCus/basedata/clueFever';
import {
  getCategories,
  getPartitionById
} from '@/api/potenCus/basedata/cluesPrameter';
export default {
  name: 'ChannelModal2',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
    };
  },
  created() {
    this.init();
  },
  methods: {
    async init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.id;
        // this.rowData.isValid = 10041002;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });

      }
    },
    createFormList() {
      return [
				{
          type: 'INPUT',
          label: '线索热度',
          fieldName: 'clueHeatName',
          placeholder: '请输入',
          rules: [{ required: true, message: "请输入线索热度", trigger: "blur"},
          { min: 1, max: 32, message: "长度在1到32个字符", trigger: "blur"}]
        },
        {
          type: 'INPUT',
          label: '描述',
          fieldName: 'remark',
          placeholder: '请输入',
        },
        {
          type: 'INPUT_NUMBER',
          label: '首次跟进时限(h)',
          fieldName: 'firstFollowTimelimit',
          initialValue: 0,
          style: { width: '250px' },
          rules: [{ required: true, message: "请选择首次跟进时限", trigger: "blur"}]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          initialValue: '',
          style: { width: '250px' },
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择是否有效', trigger: 'blur' }]
        },
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog (){ //确定
      // console.log('确定')
      this.$refs.formPanel.SUBMIT_FORM();
      // console.log(this.saveData)
      if (this.saveData !== null) {
        let res = {};
        this.saveData.level = 1;
        if (this.type === 'add') {
          res = await addClueParameter(this.saveData);
        }
        if (this.type === 'edit') {
          res = await editClueParameter(this.recordId, {...this.saveData,id:this.recordId});
        }
        if (res.resultCode === 200) {
          this.$message.success("保存成功!");
          this.closeDialog('refresh');
        }
      }
    },
    closeDialog(param) { //取消
      this.$emit('close', false, param);
    },

  }
};
</script>

<style lang="less" scoped>
</style>
